//
//  LKOpenSubordinateViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/18.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKOpenSubordinateViewController.h"
#import "LKUFPictureRow.h"
#import "LKUFPictureRowCell.h"
#import "LKUFOpenShopHeaderRow.h"
#import "LKUFOpenShopHeaderRowCell.h"
#import "LKUFOpenShopBottomButtonRow.h"
#import "LKUFOpenShopBottomButtonRowCell.h"
#import "LKOpenSubordinateUploadImageViewController.h"
#import "LKOpenSubordinateRateSettingViewController.h"
@interface LKOpenSubordinateViewController ()

@property(nonatomic,strong)UFFormView *formview;
@property(nonatomic,copy)NSString *hadSetting;
@property(nonatomic,strong)NSMutableArray *levelArra;
@property(nonatomic,copy)NSString *pictruehadSetting;
@end

@implementation LKOpenSubordinateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"代理开通";
    if (self.inforModel) {
        self.inforModel.agent_phone = self.inforModel.user_phone;
        if (self.inforModel.ali_rate.length != 0) {
            self.hadSetting = @"已设置";
        }else{
             self.hadSetting = @"";
        }
        if (self.inforModel.id_card_img.length != 0 || self.inforModel.back_id_card_img.length != 0 || self.inforModel.hand_id_card_img.length != 0 || self.inforModel.bussiness_img.length != 0 || self.inforModel.agreement_img1.length != 0 || self.inforModel.agreement_img2.length != 0 || self.inforModel.agreement_img3.length != 0 || self.inforModel.agreement_img4.length != 0) {
            self.pictruehadSetting = @"已设置";
        }else{
             self.pictruehadSetting = @"";
        }
    }else{
        self.inforModel = [[LKMySuboridateOpenInforModel alloc] init];
//        self.inforModel.agent_mode = @"0";
        self.hadSetting = @"";
        self.pictruehadSetting = @"";
        self.inforModel.referrer = [LKUserInforTool account].user_phone;
        self.inforModel.identi = @"1";
        self.inforModel.password = @"111111";
    }

    [self setNavgationRightBtn];
    [self getMyAgentLevelInformationData];
//    [self createFormView];
    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"完成" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        if (weakSelf.isEdit) {
 
        }else{
            NSDictionary *param = [weakSelf.inforModel properties_aps];
            [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateAddUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
                
            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                if ([responseObject[@"code"] isEqualToString:@"success"]) {
                    [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"mangerMyOpenSubordinateReload" object:nil];
                }else{
                    [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                }
            } failure:^(NSString * _Nullable errorMessage) {
                
            }];
        }

    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)createFormView{
    WS(weakSelf)
    self.formview = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .registerRow([LKUFOpenShopHeaderRow class], [LKUFOpenShopHeaderRowCell class])
        .registerRow([LKUFPictureRow class], [LKUFPictureRowCell class])
        .registerRow([LKUFOpenShopBottomButtonRow class], [LKUFOpenShopBottomButtonRowCell class])
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .width(WIDTH/2)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(weakSelf.inforModel.agent_name)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.agent_name = value;
                })
                .title(@"代理名称");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(weakSelf.inforModel.agent_jiancheng)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.agent_jiancheng = value;
                })
                .title(@"代理简称");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(weakSelf.inforModel.agent_phone)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.agent_phone = value;
                })
                .title(@"用户手机号");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .placeholder(@"(111111)默认密码")
                .height(RowHeightDefault)
                .value([NSString stringWithFormat:@"默认密码(%@)",weakSelf.inforModel.password])
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.password = value;
                })
                .title(@"登录密码");
            }])
            .addRow([UFPickerViewRow makePickerViewRow:^(UFPickerViewRowMaker * _Nonnull make) {
                make
                .itemArray(@[@"内部",@"外部"])
                .placeholder(@"请选择")
                .title(@"代理分类")
                .value([weakSelf.inforModel.identi isEqualToString:@"1"] ? @"内部":@"外部")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
//                    weakSelf.inforModel.identi = value;
                    if ([value isEqualToString:@"内部"]) {
                        weakSelf.inforModel.identi = @"1";
                    }else{
                        weakSelf.inforModel.identi = @"2";
                    }
                })
                .height(RowHeightDefault)
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFPickerViewRow makePickerViewRow:^(UFPickerViewRowMaker * _Nonnull make) {
                make
                .itemArray(@[@"个人",@"企业"])
                .placeholder(@"请选择")
                .title(@"代理类型")
                .value(weakSelf.inforModel.level_agent_mode)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.level_agent_mode = value;
                    if ([value isEqualToString:@"个人"]) {
                        weakSelf.inforModel.agent_mode = @"0";
                    }else{
                        weakSelf.inforModel.agent_mode = @"1";
                    }
                })
                .height(RowHeightDefault)
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFPickerViewRow makePickerViewRow:^(UFPickerViewRowMaker * _Nonnull make) {
                make
                .itemArray(self.levelArra)
                .placeholder(@"请选择")
                .title(@"代理级别")
                .value([NSString backSuboradinateTitleAccoringType:weakSelf.inforModel.agent_type])
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.hadSetting = @"已设置";
                    weakSelf.inforModel.agent_type = [NSString backSuboradinateTypeAccoringTitle:value];
                    [weakSelf.formview removeFromSuperview];
                    [weakSelf createFormView];
                })
                .height(RowHeightDefault)
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"支付费率")
                .value(weakSelf.hadSetting)
                .rowDidSelected(^(__kindof UFRow * _Nonnull row) {
                    if (weakSelf.inforModel.agent_type.length == 0) {
                        [SVProgressHUD showInfoWithStatus:@"请先选择代理级别"];
                        return ;
                    }
                    weakSelf.hadSetting = @"已设置";
                    LKOpenSubordinateRateSettingViewController *vc = [[LKOpenSubordinateRateSettingViewController alloc] init];
                    vc.inforModel = weakSelf.inforModel;
                    vc.modelBlock = ^(LKMySuboridateOpenInforModel * _Nullable model) {
                        weakSelf.inforModel = model;
                        [weakSelf.formview removeFromSuperview];
                        [weakSelf createFormView];
                    };
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                })
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .placeholder(@"请上传资料照片")
                .height(RowHeightDefault)
                .value(weakSelf.pictruehadSetting)
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .rowDidSelected(^(__kindof UFRow * _Nonnull row) {
                    LKOpenSubordinateUploadImageViewController *vc = [[LKOpenSubordinateUploadImageViewController alloc] init];
                    vc.inforModel = weakSelf.inforModel;
                    vc.modelBlock = ^(LKMySuboridateOpenInforModel * _Nullable model) {
                        weakSelf.inforModel = model;
                        if (model.id_card_img.length != 0 || model.back_id_card_img.length != 0 || model.hand_id_card_img.length != 0 || model.bussiness_img.length != 0 || model.agreement_img1.length != 0 || model.agreement_img2.length != 0 || model.agreement_img3.length != 0 || model.agreement_img4.length != 0) {
                            self.pictruehadSetting = @"已设置";
                        }
                        [weakSelf.formview removeFromSuperview];
                        [weakSelf createFormView];
                    };
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                })
                .title(@"资料照片(非必传)");
            }])
            .addRow([UFAreaPickerRow makeAreaPickerRow:^(UFAreaPickerRowMaker * _Nonnull make) {
                make
                .areaSeparator(@",")
                .height(RowHeightDefault)
                .title(@"所在地址")
                .value([NSString stringWithFormat:@"%@%@%@",[NSString judegeIsNullAndTransform:weakSelf.inforModel.province],[NSString judegeIsNullAndTransform:weakSelf.inforModel.city],[NSString judegeIsNullAndTransform:weakSelf.inforModel.county]])
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    NSArray *addressArray = [value componentsSeparatedByString:@","];
                    if (addressArray.count == 3) {
                        weakSelf.inforModel.province = addressArray[0];
                        weakSelf.inforModel.city = addressArray[1];
                        weakSelf.inforModel.county = addressArray[2];
                    }else{
                        weakSelf.inforModel.province = addressArray[0];
                        weakSelf.inforModel.county = addressArray[1];
                        if ([weakSelf.inforModel.province containsString:@"市"]) {
                            weakSelf.inforModel.county = addressArray[1];
                            weakSelf.inforModel.city = [weakSelf.inforModel.province mutableCopy];
                            weakSelf.inforModel.province = [[weakSelf.inforModel.province componentsSeparatedByString:@"市"] firstObject];
                        }
                    }
                })
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(weakSelf.inforModel.address)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.address = value;
                })
                .title(@"详细地址");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .placeholder(@"请输入推荐人名称")
                .height(RowHeightDefault)
                .value(weakSelf.inforModel.referrer)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.referrer = value;
                })
                .title(@"推荐人(非必传)");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .placeholder(@"请输入备注")
                .height(RowHeightDefault)
                .value(weakSelf.inforModel.user_note)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.inforModel.user_note = value;
                })
                .title(@"备注(非必传)");
            }]);
        }])
        .addToSuperView(self.view);
    }];
    [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
        make.bottom.equalTo(self.view);
    }];
}
-(NSMutableArray *)levelArra{
    if (_levelArra == nil) {
        _levelArra = [[NSMutableArray alloc] init];
    }
    return _levelArra;
}
/// 获取代理层级
-(void)getMyAgentLevelInformationData{
    NSDictionary *param = @{@"token":[LKUserInforTool account].token};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodGET serverUrl:ServerlUrl apiPath:MySuboradinateLevelUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            NSArray *dataArray = responseObject[@"data"];
            for (NSDictionary *dic in dataArray) {
                [self.levelArra addObject:dic[@"role_name"]];
            }
            [self createFormView];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
